package com.ruoyi.mng.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Supplier;

import com.ruoyi.mng.domain.HzBusi;
import com.ruoyi.mng.domain.HzChaChannel;
import com.ruoyi.mng.domain.HzMerMerchant;
import com.ruoyi.mng.service.IHzBusiService;
import com.ruoyi.mng.service.IHzChaChannelService;
import com.ruoyi.mng.service.IHzMerMerchantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mng.domain.HzMerBusiChaR;
import com.ruoyi.mng.service.IHzMerBusiChaRService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 商户业务渠道关系Controller
 *
 * @author chenqiang
 * @date 2023-04-28
 */
@Controller
@RequestMapping("/mng/merbusicha")
public class HzMerBusiChaRController extends BaseController {
    private String prefix = "mng/merbusicha";

    @Autowired
    private IHzMerBusiChaRService hzMerBusiChaRService;
    @Resource
    private IHzMerMerchantService hzMerMerchantService;
    @Resource
    private IHzBusiService hzBusiService;
    @Resource
    private IHzChaChannelService hzChaChannelService;


    @RequiresPermissions("mng:merbusicha:view")
    @GetMapping()
    public String merbusicha(ModelMap mmap) {
        Supplier<HzMerMerchant> merchantSupplier = HzMerMerchant::new;
        List<HzMerMerchant> hzMerMerchantList = hzMerMerchantService.selectHzMerMerchantList(merchantSupplier.get());
        ArrayList<HashMap<String, String>> merchantList = new ArrayList<>();
        hzMerMerchantList.forEach(merMerchant -> {
            HashMap<String, String> merchantMap = new HashMap<>();
            merchantMap.put("merchantId", merMerchant.getMerchantId());
            merchantMap.put("merchantName", merMerchant.getMerchantShortName());
            merchantList.add(merchantMap);
        });

        Supplier<HzBusi> busiSupplier = HzBusi::new;
        List<HzBusi> hzBusiList = hzBusiService.selectHzBusiList(busiSupplier.get());
        ArrayList<HashMap<String, String>> busiList = new ArrayList<>();
        hzBusiList.forEach(busi -> {
            HashMap<String, String> busiMap = new HashMap<>();
            busiMap.put("id", busi.getBusiId().toString());
            busiMap.put("name", busi.getBusiName());
            busiList.add(busiMap);
        });

        Supplier<HzChaChannel> channelSupplier = HzChaChannel::new;
        List<HzChaChannel> hzChaChannelList = hzChaChannelService.selectHzChaChannelList(channelSupplier.get());
        ArrayList<HashMap<String, Object>> channelList = new ArrayList<>();
        hzChaChannelList.forEach(chaChannel -> {
            HashMap<String, Object> channelMap = new HashMap<>();
            channelMap.put("channelId", chaChannel.getChannelId());
            channelMap.put("channelName", chaChannel.getChannelName());
            channelList.add(channelMap);
        });


        mmap.put("busiList", busiList);
        mmap.put("merchantList", merchantList);
        mmap.put("channelList", channelList);
        return prefix + "/merbusicha";
    }

    /**
     * 查询商户业务渠道关系列表
     */
    @RequiresPermissions("mng:merbusicha:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HzMerBusiChaR hzMerBusiChaR) {
        startPage();
        List<HzMerBusiChaR> list = hzMerBusiChaRService.selectHzMerBusiChaRList(hzMerBusiChaR);
        return getDataTable(list);
    }

    /**
     * 导出商户业务渠道关系列表
     */
    @RequiresPermissions("mng:merbusicha:export")
    @Log(title = "商户业务渠道关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HzMerBusiChaR hzMerBusiChaR) {
        List<HzMerBusiChaR> list = hzMerBusiChaRService.selectHzMerBusiChaRList(hzMerBusiChaR);
        ExcelUtil<HzMerBusiChaR> util = new ExcelUtil<HzMerBusiChaR>(HzMerBusiChaR.class);
        return util.exportExcel(list, "商户业务渠道关系数据");
    }

    /**
     * 新增商户业务渠道关系
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        Supplier<HzMerMerchant> merchantSupplier = HzMerMerchant::new;
        List<HzMerMerchant> hzMerMerchantList = hzMerMerchantService.selectHzMerMerchantList(merchantSupplier.get());
        ArrayList<HashMap<String, String>> merchantList = new ArrayList<>();
        hzMerMerchantList.forEach(merMerchant -> {
            HashMap<String, String> merchantMap = new HashMap<>();
            merchantMap.put("merchantId", merMerchant.getMerchantId());
            merchantMap.put("merchantName", merMerchant.getMerchantShortName());
            merchantList.add(merchantMap);
        });

        Supplier<HzBusi> busiSupplier = HzBusi::new;
        List<HzBusi> hzBusiList = hzBusiService.selectHzBusiList(busiSupplier.get());
        ArrayList<HashMap<String, String>> busiList = new ArrayList<>();
        hzBusiList.forEach(busi -> {
            HashMap<String, String> busiMap = new HashMap<>();
            busiMap.put("id", busi.getBusiId().toString());
            busiMap.put("name", busi.getBusiName());
            busiList.add(busiMap);
        });

        Supplier<HzChaChannel> channelSupplier = HzChaChannel::new;
        List<HzChaChannel> hzChaChannelList = hzChaChannelService.selectHzChaChannelList(channelSupplier.get());
        ArrayList<HashMap<String, Object>> channelList = new ArrayList<>();
        hzChaChannelList.forEach(chaChannel -> {
            HashMap<String, Object> channelMap = new HashMap<>();
            channelMap.put("channelId", chaChannel.getChannelId());
            channelMap.put("channelName", chaChannel.getChannelName());
            channelList.add(channelMap);
        });


        mmap.put("busiList", busiList);
        mmap.put("merchantList", merchantList);
        mmap.put("channelList", channelList);
        return prefix + "/add";
    }

    /**
     * 新增保存商户业务渠道关系
     */
    @RequiresPermissions("mng:merbusicha:add")
    @Log(title = "商户业务渠道关系", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HzMerBusiChaR hzMerBusiChaR) {
        List<HzMerBusiChaR> hzMerBusiChaRList = hzMerBusiChaRService.selectHzMerBusiChaRList(hzMerBusiChaR);
        if (null != hzMerBusiChaRList && hzMerBusiChaRList.size() != 0) {
            return AjaxResult.error("已经存在该商户业务渠道关系");
        }
        return toAjax(hzMerBusiChaRService.insertHzMerBusiChaR(hzMerBusiChaR));
    }

    /**
     * 修改商户业务渠道关系
     */
    @RequiresPermissions("mng:merbusicha:edit")
    @GetMapping("/edit/{rId}")
    public String edit(@PathVariable("rId") Long rId, ModelMap mmap) {
        Supplier<HzBusi> busiSupplier = HzBusi::new;
        List<HzBusi> hzBusiList = hzBusiService.selectHzBusiList(busiSupplier.get());
        ArrayList<HashMap<String, String>> busiList = new ArrayList<>();
        hzBusiList.forEach(busi -> {
            HashMap<String, String> busiMap = new HashMap<>();
            busiMap.put("id", busi.getBusiId().toString());
            busiMap.put("name", busi.getBusiName());
            busiList.add(busiMap);
        });

        Supplier<HzChaChannel> channelSupplier = HzChaChannel::new;
        List<HzChaChannel> hzChaChannelList = hzChaChannelService.selectHzChaChannelList(channelSupplier.get());
        ArrayList<HashMap<String, Object>> channelList = new ArrayList<>();
        hzChaChannelList.forEach(chaChannel -> {
            HashMap<String, Object> channelMap = new HashMap<>();
            channelMap.put("channelId", chaChannel.getChannelId());
            channelMap.put("channelName", chaChannel.getChannelName());
            channelList.add(channelMap);
        });

        HzMerBusiChaR hzMerBusiChaR = hzMerBusiChaRService.selectHzMerBusiChaRByRId(rId);

        mmap.put("busiList", busiList);
        mmap.put("channelList", channelList);
        mmap.put("hzMerBusiChaR", hzMerBusiChaR);
        return prefix + "/edit";
    }

    /**
     * 修改保存商户业务渠道关系
     */
    @RequiresPermissions("mng:merbusicha:edit")
    @Log(title = "商户业务渠道关系", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HzMerBusiChaR hzMerBusiChaR) {
        List<HzMerBusiChaR> hzMerBusiChaRList = hzMerBusiChaRService.selectHzMerBusiChaRList(hzMerBusiChaR);
        if (hzMerBusiChaRList != null && hzMerBusiChaRList.size() > 0) {
            HzMerBusiChaR oldHzMerBusiChaR = hzMerBusiChaRList.get(0);
            if (!oldHzMerBusiChaR.getrId().equals(hzMerBusiChaR.getrId())) {
                return AjaxResult.error("已经存在该商户业务渠道关系");
            }
        }
        return toAjax(hzMerBusiChaRService.updateHzMerBusiChaR(hzMerBusiChaR));
    }

    /**
     * 删除商户业务渠道关系
     */
    @RequiresPermissions("mng:merbusicha:remove")
    @Log(title = "商户业务渠道关系", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hzMerBusiChaRService.deleteHzMerBusiChaRByRIds(ids));
    }
}
